//
// Created by user on 2022/3/22.
//

#include <vector>
#include <algorithm>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
int maxDepath(TreeNode *root){
    if(!root)return 0;
    int left = maxDepath(root->left);
    int right = maxDepath(root->right);
    if(left==-1 || right==-1||abs(left-right)>1){
        return -1;
    }
    return max(left, right) +1;
}
bool isBalanced(TreeNode* root) {
    if (!root)return true;
    return maxDepath(root)>0;
}

